package com.example.springcloudindex.web;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserAction {

    @RequestMapping("user")
    public String user(HttpServletRequest request){
        int port = request.getServerPort();
        return "user:"+port;
    }
    @GetMapping("cookie")
    String headers(@CookieValue("Cookie") String cookie){
        return cookie;
    }
    @RequestMapping("login")
    String login(String name, String pwd, HttpSession session){
        if("yc".equals(name) && "123".equals(pwd)){
            session.setAttribute("loginedUser", name);
            return "登录成功!" + name;
        } else {
            return "登录失败!";
        }
    }

    @RequestMapping("showme")
    String showme(HttpSession session){
        return "欢迎: "+session.getAttribute("loginedUser").toString();
    }
}
